package cn.edu.njnu.opengmpback.Modeling.controller;



import cn.edu.njnu.opengmpback.Modeling.entity.dto.*;
import cn.edu.njnu.opengmpback.Modeling.service.ConceptModelService;
import cn.edu.njnu.opengmpback.common.utils.JsonResult;
import cn.edu.njnu.opengmpback.common.utils.ResultUtils;
import cn.edu.njnu.opengmpback.resource.entity.dto.CreateResourceModelDTO;
import cn.edu.njnu.opengmpback.user.entity.UserVo;
import cn.edu.njnu.opengmpback.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@RestController
@RequestMapping(value = "/conceptModel")
public class ConceptModelController {

    @Autowired
    ConceptModelService conceptModelService;

    @Autowired
    UserService userService;

    @PostMapping("/create")
    @ApiOperation(value = "创建概念模型.")
    public JsonResult createConceptModel(@RequestPart("info") CreateConceptModelDTO createConceptModelDTO, @RequestPart("imgFile") MultipartFile imgFile, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return  conceptModelService.createConceptModel(createConceptModelDTO,imgFile,userId);
    }

    @GetMapping("/open/{conceptModelId}")
    @ApiOperation(value = "打开概念模型")
    public JsonResult OpenConceptModel(@PathVariable String conceptModelId) {
        return conceptModelService.OpenConceptModel(conceptModelId);
    }


    @PostMapping("/getConceptModelList")
    @ApiOperation(value = "查询概念建模列表_基于项目")
    public JsonResult getConceptModelList(@RequestBody ConceptModelPageDTO  conceptModelFindDTO) {
        return conceptModelService.searchConceptModelList(conceptModelFindDTO);
    }


    @PostMapping("/getAllConceptModelList")
    @ApiOperation(value = "查询概念建模列表")
    public JsonResult getAllConceptModelList(@RequestBody ConceptModelPageDTO  conceptModelFindDTO) {
        return conceptModelService.searchAllConceptModelList(conceptModelFindDTO);
    }

    @PostMapping("/update/{conceptModelId}")
    @ApiOperation(value = "更新概念建模")
    public JsonResult UpdateConceptModel(@PathVariable(value = "conceptModelId") String conceptModelId, @RequestPart("info") CreateConceptModelDTO createConceptModelDTO,
                                         @RequestPart("imgFile") MultipartFile imgFile, HttpServletRequest request) throws IOException {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        return conceptModelService.UpdateConceptModel(conceptModelId,createConceptModelDTO,imgFile);
    }


    @GetMapping("/delete/{conceptModelId}")
    @ApiOperation(value = "删除概念建模")
    public JsonResult DeleteConceptModel(@PathVariable(value = "conceptModelId") String conceptModelId, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        return conceptModelService.DeleteConceptModel(conceptModelId);
    }

    /////////////////////////////////////////////////概念建模素材//////////////////////////////////////////////////////////

    @PostMapping("/backgroundRemove")
    @ApiOperation(value = "背景图片删除")
    public JsonResult backgroundRemove(@RequestPart("imgFile") MultipartFile imgFile) throws IOException {
        return conceptModelService.backgroundRemove(imgFile);
    }

    @PostMapping("/createMaterial")
    @ApiOperation(value = "创建概念建模素材")
    public JsonResult createMaterial(@RequestPart("info") CreateConceptMaterialDTO createConceptMaterialDTO,
                                     @RequestPart("imgFile") MultipartFile imgFile, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return conceptModelService.createMaterial(createConceptMaterialDTO,imgFile,userId);
    }

    @GetMapping("/getMaterialById/{materialId}")
    @ApiOperation(value = "查询概念建模素材信息")
    public JsonResult getProjectById(@PathVariable String materialId){
        return conceptModelService.getMaterialById(materialId);
    }

    @PostMapping("/getMaterialList")
    @ApiOperation(value = "查询概念建模素材列表")
    public JsonResult getMaterialList(@RequestBody ConceptMaterialPageDTO  conceptMaterialFindDTO) {
        return conceptModelService.searchMaterialList(conceptMaterialFindDTO);
    }


    @PutMapping("/updateMaterial")
    @ApiOperation(value = "更新概念建模素材")
    public JsonResult updateMaterial(@RequestPart("info") CreateConceptMaterialDTO createConceptMaterialDTO,
                                     @RequestPart("imgFile") MultipartFile imgFile,HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return conceptModelService.createMaterial(createConceptMaterialDTO,imgFile,userId);
    }

    @DeleteMapping("/deleteMaterial/{MaterialId}")
    @ApiOperation(value = "删除概念建模素材")
    public JsonResult deleteMaterial(@PathVariable("MaterialId") String materialId ) {
        JsonResult jsonResult = conceptModelService.deleteMaterial(materialId);
        return jsonResult;
    }
}
